/*---------------------------------------------------------------------------*/
/*!				\file				WindowsScan.c
				\date				march 2010
				\author				TNick

				\brief				Code to find panels to add to list
    
*/
/* ------------------------------------------------------------------------- */
/* ========================================================================= */
//
//
//
//
/*  INCLUDES    ------------------------------------------------------------ */
	#include	"Config.h"
	#include	<windows.h>
	#include	<Commctrl.h>
	
	#include	"main.h"
	#include	"Resources.h"
	
/*  INCLUDES    ============================================================ */
//
//
//
//
/*  DEFINITIONS    --------------------------------------------------------- */

/*  DEFINITIONS    ========================================================= */
//
//
//
//
/*  DATA    ---------------------------------------------------------------- */

/*  DATA    ================================================================ */
//
//
//
//
/*  FUNCTIONS    ----------------------------------------------------------- */


/* ------------------------------------------------------------------------- */
BOOL			IsAltTabWindow		(HWND hwnd)
{
	HWND		hwndWalk;
	HWND		hwndTry;
	HWND		hwndTry2 = NULL;
	
	if (GetWindowLong(hwnd,GWL_EXSTYLE) & WS_EX_TOOLWINDOW) return FALSE;
	// Start at the root owner
	hwndWalk = GetAncestor(hwnd, GA_ROOTOWNER);

	// See if we are the last active visible popup
	for (;;)
	{
		hwndTry = GetLastActivePopup(hwndWalk);
		if (hwndTry == hwndTry2) break;
		if (IsWindowVisible(hwndTry)) break;
		hwndTry2 = hwndTry;
		hwndWalk = hwndTry;
	}
	
	return hwndWalk == hwnd;
}



/* ========================================================================= */

/* ------------------------------------------------------------------------- */
BOOL			IsAlreadyPresent	(HWND hwnd)
{
	unsigned		i = 0;
	LVITEM			it;
	unsigned		icnt;
	it.mask = LVIF_PARAM;
	it.iSubItem = 0;
	
	icnt = ListView_GetItemCount(hW_Ls_L);
	while (i < icnt)
	{
		it.iItem = i;
		ListView_GetItem(hW_Ls_L, &it);
		if (it.lParam == (LPARAM)hwnd) return TRUE;
		i++;
	}
	icnt = ListView_GetItemCount(hW_Ls_R);
	i = 0;
	while (i < icnt)
	{
		it.iItem = i;
		ListView_GetItem(hW_Ls_R, &it);
		if (it.lParam == (LPARAM)hwnd) return TRUE;
		i++;
	}
	
	return FALSE;
}
/* ========================================================================= */

const TCHAR sMCI_Excl[] = TEXT("#43");

/* ------------------------------------------------------------------------- */
BOOL CALLBACK	EnumWindowsProc		(HWND hwnd,LPARAM lParam)
{
	
	LVITEM			lIns;
	unsigned		it;
	TCHAR			TxBf[256];
	TCHAR			TxBfCl[256];
	WINDOWPLACEMENT	wp;
	HICON			hi;
	
	memset(&lIns,0,sizeof(LVITEM));
	memset(&TxBf,0,sizeof(TxBf));
	memset(&TxBfCl,0,sizeof(TxBfCl));
	
	GetWindowPlacement(hwnd, &wp);
	
	GetClassName(hwnd,(TCHAR*)&TxBfCl,255);
	if (!memcmp((TCHAR*)&sMCI_Excl,(TCHAR*)&TxBfCl, sizeof(sMCI_Excl))) return TRUE;
	
	it = GetWindowText(hwnd,(TCHAR*)&TxBf,255);
	if (!memcmp(APPNAME_START,(TCHAR*)&TxBf, APPNAME_START_LEN)) return TRUE;
	
	if (AppStgs.Flags & APPFL_FLTR_NAME)
	{
		if (it <= 1) return TRUE;
		if (TxBf[0] == 0) return TRUE;
	}
	if (AppStgs.Flags & APPFL_FLTR_HID)
	{
		if (wp.showCmd  == SW_HIDE) return TRUE;
	}
	if (AppStgs.Flags & APPFL_FLTR_ALTT)
	{
		if (IsAltTabWindow(hwnd) == FALSE) return TRUE;
	}
	if (AppStgs.Flags & APPFL_FLTR_PRNT)
	{
		if (GetParent(hwnd) != NULL) return TRUE;
	}
	
	if (IsAlreadyPresent(hwnd) == TRUE) return TRUE;
	
	hi = (HICON) GetClassLong(hwnd,GCL_HICONSM);
	if (hi != NULL)
	{
		lIns.iImage = ImageList_AddIcon(hImLst,hi);
	}
	else if (AppStgs.Flags & APPFL_FLTR_ICN)
	{
		return TRUE;
	}
	
	lIns.mask = LVIF_IMAGE | LVIF_PARAM | LVIF_TEXT;
	lIns.pszText = (TCHAR*)&TxBf;
	lIns.lParam = (LPARAM)hwnd;
	it = ListView_InsertItem(hW_Ls_L, &lIns);
	
	TxBf[0] = 0;
	ListView_SetItemText(hW_Ls_L,it,1,(TCHAR*)&TxBfCl);
	return TRUE;
}
/* ========================================================================= */


/* ------------------------------------------------------------------------- */
void			RefreshVisList		(void)
{
	TCHAR			TxBf[256];
	LVITEM			it;
	HICON			hi;
	unsigned		icnt;
	unsigned		i = 0;
	
	ImageList_Destroy(hImLst);
    hImLst = ImageList_LoadImage(
				hInstance,
				MAKEINTRESOURCE(ID_BITMAP_EXC),
				SIZE_IMGINRES,SIZE_IMGINRES,
				0xFFFFFF,IMAGE_BITMAP,LR_DEFAULTCOLOR);
	
	it.mask = LVIF_IMAGE | LVIF_PARAM;
	it.iSubItem = 0;
	it.pszText = (TCHAR*)&TxBf;
	it.cchTextMax = 255;
	icnt = ListView_GetItemCount(hW_Ls_L);
	
	while (i < icnt)
	{
		it.iItem = i;
		if (ListView_GetItem(hW_Ls_L, &it) == FALSE) break;
		
		if (IsWindow((HWND)it.lParam) == FALSE)
			ListView_DeleteItem(hW_Ls_L,i);
		else
		{
			hi = (HICON) GetClassLong((HWND)it.lParam,GCL_HICONSM);
			if (hi != NULL)
			{
				it.iImage = ImageList_AddIcon(hImLst,hi);
			}
			ListView_SetItem(hW_Ls_L,&it);
			i++;
		}
	}
	
	icnt = ListView_GetItemCount(hW_Ls_R);
	i = 0;
	while (i < icnt)
	{
		it.iItem = i;
		if (ListView_GetItem(hW_Ls_R, &it) == FALSE) break;
		
		if (IsWindow((HWND)it.lParam) == FALSE)
			ListView_DeleteItem(hW_Ls_R,i);
		else
		{
			hi = (HICON) GetClassLong((HWND)it.lParam,GCL_HICONSM);
			if (hi != NULL)
			{
				it.iImage = ImageList_AddIcon(hImLst,hi);
			}
			ListView_SetItem(hW_Ls_R,&it);
			i++;
		}
	}
	
	EnumWindows(EnumWindowsProc,0);
}
/* ========================================================================= */


/*  FUNCTIONS    =========================================================== */
//
//
//
//
/* ------------------------------------------------------------------------- */
/* ========================================================================= */
